package day36;
import java.util.*;

public class A2 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int n = in.nextInt();
            long[] arr = new long[n];
            PriorityQueue<Long> queue = new PriorityQueue();
            for(int i=0;i<n;i++) {
                arr[i] = in.nextLong();
                queue.add(arr[i]);
            }
            long ret = 0;
            while(queue.size()>1) {
                long x = queue.poll();
                long y = queue.poll();
                ret += x + y;
                queue.add(x+y);
            }
            System.out.println(ret);
        }
    }
}
